import routers from "./routerMap"
import VueRouter from 'vue-router'
import Vue from 'vue'
import getUserInfo from '@/common/common'
import asyncRouters from './asyncRouterMap'

Vue.use(VueRouter)

const routes = routers

const router = new VueRouter({
  mode: 'history',
  routes
})

router.beforeEach((to,from,next)=>{
  if (sessionStorage.getItem("name") == null && localStorage.getItem("token") != null){
    getUserInfo(routerMatch(next()));
  }else{
    next();
  }
})

function routerMatch(callback = function(){}) {
  for (let index in asyncRouters) {
    console.log(asyncRouters[index]);
    router.addRoute(asyncRouters[index]);
  }
  console.log( router.getRoutes());
  callback();
}

export default router
